home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2429 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: Sun, 21 Jan 1996 12:03:59 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9601211103.AA19325@dxmint.cern.ch>
  8. References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. babycox@aol.com (BabyCox) writes:
  14.  
  15. >Here is a revision, this should work (but that's what I said last time)
  16.  
  17. You're a little bit closer this time :-)
  18.  
  19. >Boolean isPowerOfTwo(long x)
  20. >{
  21. >  long tst;
  22. >  for(tst=1<<31;tst!=0;tst>>=1)
  23. >    if (x==tst) return true;
  24. >  
  25. >  return false;
  26. >}
  27.  
  28. I've already commented about the usage of the undefined Boolean, true and
  29. false.
  30.  
  31. This new version will work _only_ on some implementations.  To make it
  32. portable, tst has to be unsigned long (x should also be unsigned long
  33. because negative numbers are _never_ powers of two :-)
  34.  
  35. If your C book hasn't taught you that the behaviour of the >> operator is
  36. implementation-defined when the first argument is negative, then it's time
  37. to read K&R2.
  38.  
  39. Dan
  40. -- 
  41. Dan Pop
  42. CERN, CN Division
  43. Email: danpop@mail.cern.ch 
  44. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  45.